package com.scala.practice.basic

import java.io.{IOException, FileOutputStream, FileInputStream}


/**
 * User: wujun
 * Date: 2014/11/12
 */
class Practice {

}
object Basic1 extends App {
  var list = List(1, 2, 3, "q", Some("xx"), None);
  list map {
    case Some(x) => println("Some: " + x);
    case s: String => println("String: " + s);
    case v: Int => println("Int: " + v);
    case None => println("None");
  }
}

object CopyBytes extends App {
  var in = None: Option[FileInputStream]
  var out = None: Option[FileOutputStream]
  try {
    in = Some(new FileInputStream("README.md"))
    out = Some(new FileOutputStream("README.md.copy"))
    var c = 0
    while ({c = in.get.read; c != -1}) {
      out.get.write(c)
    }
  } catch {
    case e: IOException => e.printStackTrace
  } finally {
    println("entered finally ...")
    if (in.isDefined) in.get.close
    if (out.isDefined) out.get.close
  }
}

object EitherUsage extends App {
  val in = "11q"
  val result: Either[String,Int] = try {
    Right(in.toInt)
  } catch {
    case e: Exception =>
      Left(in)
  }
  println(result
  match {
    case Right(x) => "Int: " + x
    case Left(x) => "Wrong in: " + x
  })
}